/**
 * @project mybatisDemo_zkingbbsxiugai
 * @Title ReplysecondController.java
 * @description 
 * @Package org.qiuqiu.controller
 * @author wangsongmao  email:wangsongmao@zkingsoft.com
 * @date  2015年5月13日 下午5:02:30
 * 
 */
package org.qiuqiu.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.qiuqiu.service.ReplySecondService;
import org.qiuqiu.vo.Post;
import org.qiuqiu.vo.Reply;
import org.qiuqiu.vo.Replysecond;
import org.qiuqiu.vo.ReplysecondExample;
import org.qiuqiu.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * @project mybatisDemo_zkingbbsxiugai
 * @Title ReplysecondController.java
 * @Package org.qiuqiu.controller
 * @author wangsongmao email:wangsongmao@zkingsoft.com
 * @date 2015年5月13日 下午5:02:30
 */
@Controller
@RequestMapping("/second")
public class ReplysecondController {
	private Logger log = Logger.getLogger(this.getClass());
	@Autowired
	private ReplySecondService replySecondService;

	@RequestMapping(value = "/secondReply", method = RequestMethod.POST)
	public void secondReply(Replysecond rsec, HttpServletResponse rs,
			HttpSession session) {
		/*
		 * Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm")
		 * .create();
		 */
		Gson g = new Gson();
		String flag = "no";
		rs.setContentType("application/json; charset=utf-8");
		PrintWriter out = null;
		try {
			out = rs.getWriter();
			User user = (User) session.getAttribute("user");
			if (user == null) {
				flag = "login";
			} else {
				rsec.setUserid(user.getUserid());
				rsec.setReplyUsername(user.getNickname());
				rsec.setReplyCreatetime(new Date());
				replySecondService.insertSelective(rsec);
				flag = "ok";
			}

		} catch (IOException e) {
			flag = "no";
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			out.write(g.toJson(flag));
			out.flush();
			out.close();
		}
	}

	@RequestMapping(value = "/getreply", method = RequestMethod.POST)
	public void getreply(@RequestParam("replyid") Integer replyid,HttpServletResponse rs) {
     
       Gson g = new Gson();	
       List<Replysecond> rslist=new ArrayList<Replysecond>();
		rs.setContentType("application/json; charset=utf-8");
		PrintWriter out = null;
		try {
			out = rs.getWriter();		
			   rslist=replySecondService.getReplyById(replyid);
		} catch (IOException e) {
			log.error(e);
			e.printStackTrace();
		}catch (Exception e) {
		    log.error(e);
		}finally{
			out.write(g.toJson(rslist));
			out.flush();
			out.close();
		}
			
}
}
